package com.blogSystem.admin.service;


import com.blogSystem.admin.dao.AdminDao;
import com.blogSystem.admin.pojo.Admin;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder encoder;



    public List<Admin> findAll(){
        return adminDao.findAll();
    }

    /**
     * 查找
     * @param id
     * @return
     */
    public Admin findById(String id){
        return adminDao.findById(id).get();
    }

    /**
     * 增加
     * @param admin
     */
    public void setAdmin(Admin admin){
        admin.setId(idWorker.nextId()+"");
        //加密
        admin.setPassword(encoder.encode(admin.getPassword()));
        admin.setRole(1);

        System.out.println(adminDao.save(admin));

    }

    /**
     * 根据名字查询
     * @param admin
     * @return
     */
    public Admin searchAdmin(Admin admin){
        return adminDao.findAdminByName(admin.getName());
    }

    public Admin login(Admin admin ){
        //现根据用户名查询对象
        Admin adminlog= adminDao.findAdminByName(admin.getName());

        //拿数据库中密码和用户密码看是否匹配
        if(adminlog!=null&&encoder.matches(admin.getPassword(),adminlog.getPassword())){
            return adminlog;
        }
        return null;
    }

    public void delete(String id){
        adminDao.deleteById(id);
    }

    /**
     * 修改名字
     * @param id
     * @param newAdmin
     * @return
     */
    public boolean changeName(String id,Admin newAdmin){
        Admin oldAdmin=adminDao.findById(id).get();
        if(newAdmin.getName()!=null&&!newAdmin.getName().equals("")){
            if(adminDao.findAdminByName(newAdmin.getName())==null){
                oldAdmin.setName(newAdmin.getName());
                adminDao.save(oldAdmin);
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

    /**
     * 修改密码
     * @param id
     * @param newAdmin
     * @return
     */
    public boolean changePass(String id,Admin newAdmin){
        Admin oldAdmin=adminDao.findById(id).get();
        if(newAdmin.getPassword()!=null&&!newAdmin.getPassword().equals("")){
            String newPass=encoder.encode(newAdmin.getPassword());
            oldAdmin.setPassword(newPass);
            adminDao.save(oldAdmin);
            return true;
        }
        return false;
    }

    /**
     * 根据名字模糊条件查询
     * @param admin
     * @return
     */
    public Page<Admin> findSearch(Admin admin, int page, int size){
        Pageable pageable= PageRequest.of(page-1,size);
        return adminDao.findAll(new Specification<Admin>(){
            @Override
            public Predicate toPredicate(Root<Admin> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                if(admin.getName()!=null&&!"".equals(admin.getName())) {
                    Predicate predicate = criteriaBuilder.like(root.get("name").as(String.class), "%" + admin.getName() + "%");
                    return predicate;
                }
                return null;
            }
        },pageable );
    }
    /**
     * 查询所有
     *
     * @return
     */
    public Page<Admin> findAll( int page, int size){
        Pageable pageable= PageRequest.of(page-1,size);
        return adminDao.findAll(pageable) ;
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteList(List<String> ids){
        adminDao.deleteAdminsByIdIn(ids);
    }
    public Admin dologin(String name){
        Admin admin=adminDao.findAdminByName(name);
        if(admin==null){

            throw new UsernameNotFoundException(name);
        }
        return new Admin(admin.getName(),admin.getPassword());
    }
}
